package com.live.adapter;

import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.live.activity.LiveFriendHomeActivity;
import com.live.activity.R;
import com.live.common.Constant;
import com.live.common.ImageDownloader;
import com.live.task.LiveFriend;
import com.live.tools.LiveTools;

public class LiveHomeFriendSA extends SimpleAdapter {

	public List<Map<String, Object>> list = null;
	public Context context = null;
	public String userId = null;
	public int index = 0;
	public List<Map<String, Object>> items;

	private final ImageDownloader imageDownloader = new ImageDownloader();

	public LiveHomeFriendSA(Context context, List<Map<String, Object>> items, int resource, String[] from, int[] to,
			String userId) {
		super(context, items, resource, from, to);
		this.list = items;
		this.context = context;
		this.userId = userId;
		this.items = items;
	}

	@Override
	@SuppressWarnings("unchecked")
	public View getView(final int position, View convertView, ViewGroup parent) {

		if (convertView == null) {
			LayoutInflater inflater = ((Activity) context).getLayoutInflater();
			convertView = inflater.inflate(R.layout.home_friend_list, null, true);
		}
		if (position <= list.size()) {

			final Map<String, Object> obj = (Map<String, Object>) getItem(position);

			// 设置姓名
			TextView userId = (TextView) convertView.findViewById(R.id.userId);
			userId.setText((String) obj.get("userId"));

			// 设置签名
			TextView userSign = (TextView) convertView.findViewById(R.id.userSign);
			if (!"".equals(obj.get("userSign"))) {
				userSign.setText(obj.get("userSign").toString());
			} else {
				userSign.setText(R.string.user_sign_no);
			}

			// 设置城市
			TextView userCity = (TextView) convertView.findViewById(R.id.userCity);
			if (!"".equals(obj.get("userCity").toString())) {
				userCity.setText((String) obj.get("userCity"));
			} else {
				userCity.setText(R.string.user_city_no);
			}

			// 设置年龄
			TextView userAge = (TextView) convertView.findViewById(R.id.userAge);
			userAge.setText((String) obj.get("userAge"));

			// 设置性别
			TextView userSex = (TextView) convertView.findViewById(R.id.userSex);
			if (Constant.USER_SEX_MAN.equals((String) obj.get("userSex"))) {
				userSex.setText("♂");
				userSex.setTextColor(0xff00CCFF);
				userAge.setTextColor(0xff00CCFF);
			} else {
				userSex.setText("♀");
				userSex.setTextColor(0xffFF6699);
				userAge.setTextColor(0xffFF6699);
			}

			// 设置头像
			ImageView userPic = (ImageView) convertView.findViewById(R.id.userPic);
			if (Constant.Y.equals(obj.get("userPic"))) {
				String imageUrl = Constant.SITE_URL + Constant.SITE_AVATAR_PATH + obj.get("uId") + ".jpg";
				imageDownloader.download(imageUrl, userPic, (String) obj.get("userPicVer"));
			} else {
				userPic.setImageResource(R.drawable.default_pic);
			}
			userPic.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					Intent intent = new Intent();
					intent.setClass(context, LiveFriendHomeActivity.class);
					Bundle bundle = new Bundle();
					bundle.putString("f", (String) obj.get("f"));
					bundle.putString("uId", (String) obj.get("uId"));
					bundle.putString("userId", (String) obj.get("userId"));
					bundle.putString("userSign", (String) obj.get("userSign"));
					bundle.putString("userSex", (String) obj.get("userSex"));
					bundle.putString("userAge", (String) obj.get("userAge"));
					if (!"null".equals(obj.get("userCity").toString())) {
						bundle.putString("userCity", (String) obj.get("userCity"));
					} else {
						bundle.putString("userCity", context.getResources().getString(R.string.user_city_no));
					}
					bundle.putString("userPic", (String) obj.get("userPic"));
					bundle.putString("userPicVer", (String) obj.get("userPicVer"));
					intent.putExtras(bundle);
					context.startActivity(intent);
				}
			});

			// 设置删除按钮
			ImageView delBtn = (ImageView) convertView.findViewById(R.id.userFriendDel);
			delBtn.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {

					// 生成确认对话框
					AlertDialog.Builder builder = new AlertDialog.Builder(context);
					builder.setTitle(R.string.delete_confirm_info);
					builder.setIcon(android.R.drawable.ic_dialog_info);
					builder.setPositiveButton(R.string.delete_btn, new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {
							String url = Constant.SITE_URL + "live_friend.php?locale=" + LiveTools.getLocaleLanguage()
									+ "&";
							url += "fId=" + (String) obj.get("fId") + "&fState=" + Constant.ACTION_REMOVE;
							Bundle bundle = new Bundle();
							bundle.putString("action", Constant.ACTION_REMOVE);
							bundle.putInt("index", position);
							LiveFriend task = new LiveFriend(context, url, list, LiveHomeFriendSA.this, null, null,
									null, null);
							task.execute(bundle);
						}
					});
					builder.setNegativeButton(R.string.cancel_btn, new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {
						}
					});
					builder.create().show();
				}
			});
		}
		return convertView;
	}

	public ImageDownloader getImageDownloader() {
		return imageDownloader;
	}
}